package team.hyznrj.studentsys.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import team.hyznrj.studentsys.dao.repository.account.FunctionInfoRepository;
import team.hyznrj.studentsys.dao.repository.account.PowerFunctionTableRepository;
import team.hyznrj.studentsys.entity.account.FunctionInfo;
import team.hyznrj.studentsys.entity.account.PowerFunctionTable;
import team.hyznrj.studentsys.enums.AccountEnum;
import team.hyznrj.studentsys.exception.AccountException;
import team.hyznrj.studentsys.service.FunctionService;

import java.util.List;
import java.util.Set;

/**
 * @author ReMidDream
 * @date 2018-05-07 21:59
 **/
@Service
@Slf4j
public class FunctionServiceImpl implements FunctionService {

    @Autowired
    private FunctionInfoRepository functionInfoRepository;
    @Autowired
    private PowerFunctionTableRepository powerFunctionTableRepository;

    @Override
    public List<FunctionInfo> findAll() {
        return functionInfoRepository.findAll();
    }

    @Override
    public FunctionInfo findByFunctionId(String functionId) {
        return functionInfoRepository.findOne(functionId);
    }

    @Override
    public List<FunctionInfo> findByFunctionIds(Set<String> functionIds) {
        return functionInfoRepository.findAll(functionIds);
    }

    @Override
    public void deleteFunctionInfo(String id) {
        List<PowerFunctionTable> powerFunctionTables = powerFunctionTableRepository.findByIdPftFunctionId(id);

        if (powerFunctionTables.size() > 0) {
            log.error(" @删除功能 : 此功能已被权限引用 functionId={}",id);
            throw new AccountException(AccountEnum.TABLE_ERROR);
        }

        functionInfoRepository.delete(id);
    }

    @Override
    public void insertUpdateFunctionInfo(FunctionInfo functionInfo) {
        functionInfoRepository.save(functionInfo);
    }


}
